<?php
/**
 * @package Joostina
 * @copyright Авторские права (C) 2008-2010 Joostina team. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * Joostina! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл help/copyright.php.
 */

// запрет прямого доступа
defined( '_VALID_MOS' ) or die();

$content ="";
GLOBAL $time_zone;

$moduleclass_sfx = $params->get('moduleclass_sfx');
$header_add = $params->get ('add_header');
$text_header = $params->get ('header_text');
$number = $params->get('number_posts', 5);
$max = $params->get('max_length', 100);
$name = $params->get('name', 0);
$link = $params->get('link', 0);
$language = $params->get('lang');
$txt_font_color = $params->get('text_font_color');
$post_option = $params->get('posts_list_options');
$options_id = $params->get('option_id');
$time_date = $params->get('time_date');
$time_zone = $params->get('time_zone');
$params->def('template', 'default.php');

//Подключаем вспомогательный класс
$module->get_helper($mainframe);

if($post_option == 1) {
		$database->setQuery("SELECT #__forum_topics.num_replies as replies,#__forum_topics.last_post_id,#__forum_topics.subject,#__forum_topics.last_poster,#__forum_topics.last_post FROM #__forum_forums INNER JOIN #__forum_topics ON (#__forum_forums.id = #__forum_topics.forum_id) WHERE #__forum_forums.cat_id IN ($options_id) ORDER BY #__forum_topics.last_post DESC LIMIT $number");
	}
	else if($post_option == 2) {
		$database->setQuery("SELECT #__forum_topics.num_replies as replies,#__forum_topics.last_post_id,#__forum_topics.subject,#__forum_topics.last_poster,#__forum_topics.last_post FROM #__forum_forums INNER JOIN #__forum_topics ON (#__forum_forums.id = #__forum_topics.forum_id) WHERE #__forum_forums.cat_id NOT IN ($options_id) ORDER BY #__forum_topics.last_post DESC LIMIT $number");
	}
	else if($post_option == 3) {
		$database->setQuery("SELECT #__forum_topics.num_replies as replies,#__forum_topics.last_post_id,#__forum_topics.subject,#__forum_topics.last_poster,#__forum_topics.last_post FROM #__forum_forums INNER JOIN #__forum_topics ON (#__forum_forums.id = #__forum_topics.forum_id) WHERE #__forum_forums.id IN ($options_id) ORDER BY #__forum_topics.last_post DESC LIMIT $number");
	}
	else if($post_option == 4) {
		$database->setQuery("SELECT #__forum_topics.num_replies as replies,#__forum_topics.last_post_id,#__forum_topics.subject,#__forum_topics.last_poster,#__forum_topics.last_post FROM #__forum_forums INNER JOIN #__forum_topics ON (#__forum_forums.id = #__forum_topics.forum_id) WHERE #__forum_forums.id NOT IN ($options_id) ORDER BY #__forum_topics.last_post DESC LIMIT $number");
	}
	else {
		$database->setQuery("SELECT #__forum_topics.num_replies as replies,#__forum_topics.last_post_id,#__forum_topics.subject,#__forum_topics.last_poster,#__forum_topics.last_post FROM #__forum_forums INNER JOIN #__forum_topics ON (#__forum_forums.id = #__forum_topics.forum_id) ORDER BY #__forum_topics.last_post DESC LIMIT $number");
	}

$list = $database->loadObjectList();

	$query = "SELECT id FROM #__menu WHERE link='index.php?option=com_forum'";
	$yarbb_Itemid = $database->setQuery($query)->loadResult();
	
	$yarbb_path = "index.php?option=com_forum&Itemid=".$yarbb_Itemid;

if ($header_add == 1) {
    $content .= "<font color=$txt_font_color size=\"2\"><br/>$text_header<br></font><br/>";
}

function format_time_module_yarbb($timestamp, $date_only = false) {
    GLOBAL $time_zone;
	if ($timestamp == '')
		return '';

	$diff = ($time_zone) * 3600;
	$timestamp += $diff;
	$now = time();
	$date = date('d.m.Y', $timestamp);
	$today = date('d.m.Y', $now+$diff);
	$yesterday = date('d.m.Y', $now+$diff-86400);
	if ($date == $today)
		$date = 'Сегодня';
	else if ($date == $yesterday)
		$date = 'Вчера';
	if (!$date_only)
		return $date.' '.date('H:i', $timestamp);
	else
		return $date;
}

foreach ($list as $item)
{
    $thread = $item->subject;
    $length = strlen($thread);
    if ($length > $max) {
        $thread = substr($thread, 0, $max).'...';
    }
	
//*****************
// постраничка
//$database = database::getInstance();
	if ($my->id) {
		$sql="SELECT `disp_posts` FROM #__forum_users WHERE `jos_id`=".$my->id;
		$disp_posts=$database->setQuery($sql)->loadResult();
		} 
	if (($disp_posts=='NULL') || (!$disp_posts) || ($disp_posts == 0)) {	
		$sql="SELECT `conf_value` FROM #__forum_config WHERE `conf_name`='o_disp_posts_default'";	
		$disp_posts=$database->setQuery($sql)->loadResult();
		}

		$num_pages 		= ceil(($item->replies + 1) / $disp_posts);
		//$page			= ($page > $num_pages) ? 1 : $page;
		//$start_from		= $disp_posts * ($page - 1);
		//$lastpage		=
		$item_link	= sefRelToAbs($yarbb_path ."&amp;task=viewtopic&amp;pid=".$item->last_post_id . '&amp;page=' .$num_pages)."#p".$item->last_post_id;
/*	} else	{
		$item_link	= sefRelToAbs($yarbb_path ."&amp;task=viewtopic&amp;pid=".$item->last_post_id . '&amp;page=' .$num_pages)."#p".$item->last_post_id;	
	}*/

//*****************	
    $content .= "<div class=\"forum_latest_item\"><img src=\"modules/mod_yarbb_latest/folder.gif\" width=\"16\" height=\"16\" border=\"0\">&nbsp;<a class=\"".$moduleclass_sfx."\" href=\"".$item_link."\">".stripslashes($thread)." (".$item->replies.")"."</a><br/>";

    if($name == 1) {
        $content .= "<font color=$txt_font_color size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">$language  $item->last_poster&nbsp;</font>";
    }
    else {
        $content .= "";
    }

    if($time_date == 1) {
      	//$content .= " ".$language." ".$item->last_poster."</font><br/>";
        $content .=  "<font color=$txt_font_color size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">|&nbsp;". format_time_module_yarbb($item->last_post) . "&nbsp;</font><br/></div>";
		      	echo $xxx;
    }
    else {
      	$content .= "";
    }
}